package com.ruoyi.framework.web.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.wq.domain.WqQuestion;
import com.ruoyi.wq.domain.WqQuestionReply;
import com.ruoyi.wq.domain.vo.WqQuestionVO;
import com.ruoyi.wq.service.IWqQuestionReplyService;
import com.ruoyi.wq.service.IWqQuestionService;
import com.ruoyi.wx.domain.WxMaForm;
import com.ruoyi.wx.domain.WxMaMsgForm;
import com.ruoyi.wx.domain.WxMpMsgForm;
import com.ruoyi.wx.service.WxMaUserService;
import com.ruoyi.wx.service.WxMsgService;

import cn.hutool.core.date.DateUtil;

@Component
public class QuestionService {
	
	@Autowired
	private ISysUserService sysUserService;
	
	@Autowired
    private WxMaUserService wxMaUserService;
	
	@Autowired
	private IWqQuestionService wqQuestionService;
	
	@Autowired
	private IWqQuestionReplyService wqQuestionReplyService;
	
	@Autowired
    private WxMsgService wxMsgService;

	public WqQuestion add(WqQuestion wqQuestion) {
		if(wqQuestion.getUserId() == null){
			throw new ServiceException("缺少参数");
		}
		if(StringUtils.isBlank(wqQuestion.getMessage())){
			throw new ServiceException("内容不能为空");
		}
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		if(wqQuestion.getUserId().equals(user.getUserId())){
			throw new ServiceException("不能向自己提问");
		}
		
		WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqQuestion.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		throw new ServiceException("内容涉嫌违规");
    	}
		
		wqQuestion.setSendUserId(user.getUserId());
		wqQuestion.setAvatar(user.getAvatar());
		wqQuestion.setNickName(user.getNickName());
		wqQuestion.setSex(user.getSex());
		
		SysUser fromUser = sysUserService.getById(wqQuestion.getUserId());
		
		WxMaMsgForm form = new WxMaMsgForm();
		form.setAppId(fromUser.getAppId());
		form.setOpenId(fromUser.getOpenId());
		Map<String,String> map = new HashMap<>();
		map.put("phrase1", "提问通知");
		map.put("date2", DateUtil.now());
		map.put("name3", "坦白局");
		map.put("thing4", "您好，您收到一条提问");
		form.setDataMap(map);
		form.setPage("/pages/question/play");
		boolean flag = wxMsgService.sendSubscribeMsg(form);
		if(!flag){
			// 发送微信订阅消息
			WxMpMsgForm msgForm = new WxMpMsgForm();
			msgForm.setWxId(fromUser.getWxId());
			msgForm.setAppId(fromUser.getAppId());
			msgForm.setOpenId(fromUser.getWxOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("first", "您好，您收到一条提问");
			dataMap.put("keyword1", "坦白局");
			dataMap.put("keyword2", DateUtil.now());
			dataMap.put("keyword3", "您好，您收到一条提问");
			dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("/pages/question/play");
			flag = wxMsgService.sendTemplateMsg(msgForm);
			if(!flag){
				fromUser.setIsSend("1");
				sysUserService.updateById(fromUser);
			}
		}
		
		wqQuestionService.save(wqQuestion);
		return wqQuestion;
	}

	public Boolean remove(String id) {
		WqQuestion question = wqQuestionService.getById(id);
		if(question.getUserId().equals(SecurityUtils.getUserId())){
			return wqQuestionService.removeById(id);
		}
		return false;
	}

	public WqQuestionReply addReply(WqQuestionReply wqQuestionReply) {
		if(StringUtils.isBlank(wqQuestionReply.getQuestionId())){
			throw new ServiceException("缺少参数");
		}
		
		if(StringUtils.isBlank(wqQuestionReply.getMessage())){
			throw new ServiceException("内容不能为空");
		}
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		
		WxMaForm wxMaForm = new WxMaForm();
    	wxMaForm.setAppId(user.getAppId());
    	wxMaForm.setMessage(wqQuestionReply.getMessage());
    	wxMaForm.setOpenId(user.getOpenId());
    	Boolean checkMessage = wxMaUserService.checkMessage(wxMaForm);
    	if(!checkMessage){
    		throw new ServiceException("内容涉嫌违规");
    	}
		wqQuestionReply.setSendUserId(user.getUserId());
		wqQuestionReply.setAvatar(user.getAvatar());
		wqQuestionReply.setNickName(user.getNickName());
		wqQuestionReply.setSex(user.getSex());
		wqQuestionReplyService.save(wqQuestionReply);
		
		WqQuestion question = wqQuestionService.getById(wqQuestionReply.getQuestionId());
		question.setReplyStatus("1");
		wqQuestionService.updateById(question);
		
		SysUser sendUser = sysUserService.getById(question.getSendUserId());
		
		WxMaMsgForm form = new WxMaMsgForm();
		form.setAppId(sendUser.getAppId());
		form.setOpenId(sendUser.getOpenId());
		Map<String,String> map = new HashMap<>();
		map.put("phrase1", "回复通知");
		map.put("date2", DateUtil.now());
		map.put("name3", "坦白局");
		map.put("thing4", "您好，您收到一条回复");
		form.setDataMap(map);
		form.setPage("/pages/question/index?my=true&shareUserId="+user.getUserId());
		boolean flag = wxMsgService.sendSubscribeMsg(form);
		if(!flag){
			// 发送微信订阅消息
			WxMpMsgForm msgForm = new WxMpMsgForm();
			msgForm.setWxId(sendUser.getWxId());
			msgForm.setAppId(sendUser.getAppId());
			msgForm.setOpenId(sendUser.getWxOpenId());
			Map<String,String> dataMap = new HashMap<>();
			dataMap.put("first", "您好，您收到一条回复");
			dataMap.put("keyword1", "坦白局");
			dataMap.put("keyword2", DateUtil.now());
			dataMap.put("keyword3", "您好，您收到一条回复");
			dataMap.put("remark", "本条信息是站内信通知，祝您早日脱单！点击查看详情");
			msgForm.setDataMap(dataMap);
			msgForm.setPage("/pages/question/index?my=true&shareUserId="+user.getUserId());
			wxMsgService.sendTemplateMsg(msgForm);
		}
		
		return wqQuestionReply;
	}

	public AjaxResult getPageList(WqQuestionVO wqQuestion) {
		if(wqQuestion.getUserId() == 11737042){
			SysUser user = sysUserService.getRandomAskUser(wqQuestion.getSendUserId());
			wqQuestion.setUserId(user.getUserId());
		}
		TableDataInfo page = wqQuestionService.getPageList(wqQuestion);
		SysUser sysUser = sysUserService.getById(wqQuestion.getUserId());
		SysUser user = new SysUser();
		user.setAvatar(sysUser.getAvatar());
		user.setSex(sysUser.getSex());
		user.setNickName(sysUser.getNickName());
		user.setUserId(sysUser.getUserId());
		
		AjaxResult ajax = AjaxResult.success();
		ajax.put("page", page);
		ajax.put("user", user);
		return ajax;
	}

	public AjaxResult getMyPageList(WqQuestionVO wqQuestion) {
		wqQuestion.setUserId(SecurityUtils.getUserId());
		TableDataInfo page = wqQuestionService.getMyPageList(wqQuestion);
		SysUser sysUser = sysUserService.getById(wqQuestion.getUserId());
		SysUser user = new SysUser();
		user.setAvatar(sysUser.getAvatar());
		user.setSex(sysUser.getSex());
		user.setNickName(sysUser.getNickName());
		user.setUserId(sysUser.getUserId());
		
		AjaxResult ajax = AjaxResult.success();
		ajax.put("page", page);
		ajax.put("user", user);
		return ajax;
	}

	public void openUser(String id) {
		WqQuestion question = wqQuestionService.getById(id);
		question.setCheckStatus("1");
		wqQuestionService.updateById(question);
	}

	public void openAllUser() {
		SysUser user = sysUserService.getById(SecurityUtils.getUserId());
		
		if(user.getVipEndTime() == null || user.getVipEndTime().compareTo(new Date()) < 0){
			throw new ServiceException("解密全部，请开通会员",788);
		}
		
		UpdateWrapper<WqQuestion> updateWrapper = new UpdateWrapper<>();
		WqQuestion entity = new WqQuestion();
		entity.setUserId(SecurityUtils.getUserId());
		entity.setCheckStatus("0");
		updateWrapper.setEntity(entity);
		
		WqQuestion question = new WqQuestion();
		question.setCheckStatus("1");
		wqQuestionService.update(question, updateWrapper);
	}
	

}
